package com.wu.leetcode.leetcode6;

import java.util.ArrayList;
import java.util.List;

/**
 * Leetcode6-Z型变换
 * L   C   I   R
 * E T O E S I I G
 * E   D   H   N
 * LEETCODEISHIRING 3
 *
 * LCIRETOESIIGEDHN
 *
 * L     D     R
 * E   O E   I I
 * E C   I H   N
 * T     S     G
 *LDREOEIIECIHNTSG
 * @author lynn
 * @date 2020/8/20 - 16:00
 */
class Solution {
    public static String convert(String s, int numRows) {
        if(numRows < 2) {
            return s;
        }
        List<StringBuilder> rows = new ArrayList<StringBuilder>();
        for(int i = 0; i < numRows; i++) {
            rows.add(new StringBuilder());
        }
        int i = 0, flag = -1;
        for(char c : s.toCharArray()) {
            rows.get(i).append(c);
            if(i == 0 || i == numRows -1) {
                flag = - flag;
            }
            i += flag;
        }
        StringBuilder res = new StringBuilder();
        for(StringBuilder row : rows) {
            res.append(row);
        }
        return res.toString();
    }

    public static void main(String[] args) {
        String leetcodeishiring = convert("LEETCODEISHIRING", 4);
        System.out.println(leetcodeishiring);
    }
}
